﻿using System;
using System.Collections.Generic;

namespace ProblemsSet
{
    public class Problem_79 : BaseProblem
    {
        public override object GetResult()
        {
            
            #region Init

            var val = new[]{
                319,
                680,
                180,
                690,
                129,
                620,
                762,
                689,
                762,
                318,
                368,
                710,
                720,
                710,
                629,
                168,
                160,
                689,
                716,
                731,
                736,
                729,
                316,
                729,
                729,
                710,
                769,
                290,
                719,
                680,
                318,
                389,
                162,
                289,
                162,
                718,
                729,
                319,
                790,
                680,
                890,
                362,
                319,
                760,
                316,
                729,
                380,
                319,
                728,
                716
};

            #endregion


            var valst = new List<string>();
            var hs = new HashSet<char>();
            foreach (var i in val)
            {
                valst.Add(i.ToString());
                foreach (var c in i.ToString())
                {
                    if (!hs.Contains(c)) hs.Add(c);
                }
            }

            var cur = ((long)(Math.Pow(10, hs.Count-1))).ToString();
            while(true)
            {
                var ex = true;
                foreach (var s in valst)
                {
                    var prev = -1;
                    foreach (var c in s)
                    {
                        if ((prev = cur.IndexOf(c, prev + 1)) >= 0) continue;
                        ex = false;
                        break;
                    }
                    if (!ex) break;
                }
                if (ex) return cur;
                cur = MathLogic.SummString(cur, "1");
            }

        }

        public override string Problem
        {
            get
            {
                return @"A common security method used for online banking is to ask the user for three random characters from a passcode. For example, if the passcode was 531278, they may asked for the 2nd, 3rd, and 5th characters; the expected reply would be: 317.

The text file, keylog.txt, contains fifty successful login attempts.

Given that the three characters are always asked for in order, analyse the file so as to determine the shortest possible secret passcode of unknown length.";
            }
        }

        public override bool IsSolved
        {
            get
            {
                return true;
            }
        }

        public override object Answer
        {
            get
            {
                return "73162890";
            }
        }
    }
}
